// 文件: CardDisplay.ts
import { _decorator, Component, Node, Sprite, Label, Button, Prefab, Enum } from 'cc';
import { UserData } from './UserData';
import { EventManager } from './EventManager';
import { CardType, EventName } from './Const';
import { GameManager } from './GameManager';

const { ccclass, property } = _decorator;

@ccclass('CardDisplay')
export class CardDisplay extends Component {
    @property({ type: Enum(CardType) })
    private cardType: CardType = CardType.ADD_ROLE;

    @property
    private price: number = 100;

    @property({ type: Node, tooltip: "锁图标节点" })
    private lockIcon: Node = null;

    @property({ type: Sprite, tooltip: "圆形进度条Sprite" })
    private progressBar: Sprite = null;

    @property({ type: Button, tooltip: "卡牌自身的按钮组件" })
    private cardButton: Button = null;

    @property({ type: Prefab, tooltip: "这张卡牌对应的单位预制体（如弓兵、刀兵）" })
    public unitPrefab: Prefab = null;

    private isUnlocked: boolean = false;

    onLoad() {
        // 游戏开始时，默认是锁定状态
        this.lockCard();
        // 监听按钮点击事件
        this.cardButton.node.on(Button.EventType.CLICK, this.onCardClicked, this);

        EventManager.I.on(EventName.COIN_CHANGED, this.setProgress, this);
    }

    onDestroy() {
        EventManager.I.off(EventName.COIN_CHANGED, this.setProgress, this);
    }

    /**
     * 锁定卡牌的显示状态
     */
    public lockCard() {
        this.isUnlocked = false;
        if (this.lockIcon) this.lockIcon.active = true;
        if (this.progressBar) this.progressBar.node.active = false; // 隐藏进度条
        if (this.cardButton) this.cardButton.interactable = false; // 按钮不可交互
    }

    /**
     * 解锁卡牌的逻辑
     */
    public unlockCard() {
        if (this.isUnlocked) return; // 如果已经解锁，就不用重复执行

        this.isUnlocked = true;
        console.log(`卡牌 ${ this.node.name } 已解锁!`);

        if (this.lockIcon) this.lockIcon.active = false; // 隐藏锁
        if (this.progressBar) {
            this.progressBar.node.active = true;
            this.setProgress(); // 更新进度条显示
        }
        if (this.cardButton) this.cardButton.interactable = true; // 按钮变为可交互
    }

    public setProgress(): void {
        let progress: number = UserData.Coin / this.price;
        if (progress >= 1) progress = 1;
        this.progressBar.fillRange = 1 - progress;
    }

    /**
     * 当卡牌被点击时调用
     */
    private onCardClicked() {
        if (!this.isUnlocked) {
            console.log("卡牌尚未解锁，无法点击。");
            return;
        }

        if (!UserData.spendCoin(this.price)) {
            console.log("金币不足，无法使用该卡牌。");
            return;
        }

        switch (this.cardType) {
            case CardType.ADD_ROLE:
                this.onAddRoleCardClicked();
                break;
            case CardType.ADD_ATTACK_SPEED:
                this.onAddRoleAttackSpeedClicked();
                break;
            case CardType.ADD_TRAJECTORY:
                this.onAddTrajectoryClicked();
                break;

        }
    }

    private onAddRoleCardClicked() {
        GameManager.Instance.addRole();
    }

    private onAddRoleAttackSpeedClicked() {
        UserData.addAttackScale(0.2);
    }

    private onAddTrajectoryClicked() {
        UserData.addTrajectory(3);
    }
}
